package com.freesky.biz.icbc.service.impl;

import com.freesky.biz.icbc.domain.TScanReceive;
import com.freesky.biz.icbc.domain.TScanRecord;
import com.freesky.biz.icbc.domain.TSocialCard;
import com.freesky.biz.icbc.mapper.ScanReceiveMapper;
import com.freesky.biz.icbc.mapper.SocialCardMapper;
import com.freesky.biz.icbc.service.IScanReceiveService;
import com.freesky.biz.icbc.service.ISocialCardService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScanReceiveServiceImpl implements IScanReceiveService {

    @Autowired
    private ScanReceiveMapper scanReceiveMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TScanReceive> findList(TScanReceive scanReceive) {
        List<TScanReceive> scanReceiveList = scanReceiveMapper.findList(scanReceive);
        for(int i=0;i<scanReceiveList.size();i++){
            TScanReceive scanReceiveData = scanReceiveList.get(i);
            if("101512".equalsIgnoreCase(scanReceiveData.getBusiCode())){
                if("1".equalsIgnoreCase(scanReceiveData.getCouponType())){
                    scanReceiveData.setName("新客");
                } else if("2".equalsIgnoreCase(scanReceiveData.getCouponType())){
                    scanReceiveData.setName("分期");
                }
            }
            scanReceiveList.set(i,scanReceiveData);
        }
        return scanReceiveList;
    }

    @Override
    public AjaxResult exportFile(TScanReceive scanReceive) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("SCAN_RECEIVE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(scanReceive, new TScanReceive(), IScanReceiveService.class,
                "findList", "扫码领取",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TScanReceive> selectPsbcList(TScanReceive scanReceive) {
        return scanReceiveMapper.selectPsbcList(scanReceive);
    }

    @Override
    public AjaxResult exportPsbcFile(TScanReceive scanReceive) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("SCAN_PSBC_RECEIVE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(scanReceive, new TScanReceive(), IScanReceiveService.class,
                "selectPsbcList", "扫码领取",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }


    @Override
    public List<TScanRecord> selectRecordList(TScanRecord entity) {
        return scanReceiveMapper.selectRecordList(entity);
    }

    @Override
    public AjaxResult exportRecordFile(TScanRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("SCAN_RECORD");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TScanRecord(), IScanReceiveService.class,
                "selectRecordList", "兑换记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
